package main.java.easy;

import java.util.ArrayList;

/**
 * @Author: 郑国荣
 * @Date: 2023-07-14-12:25
 * @Description: 折半查找、二分查找
 */
public class SearchInsert {
    public int searchInsert(int[] nums,int target){
        int left = 0;
        int right = nums.length - 1;

        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        // 如果没有找到，则返回应该插入的位置
        return left;
    }
}
